package com.powernode.rules.service.impl;


import com.powernode.model.form.rules.RewardRuleRequest;
import com.powernode.model.form.rules.RewardRuleRequestForm;
import com.powernode.model.vo.rules.RewardRuleResponse;
import com.powernode.model.vo.rules.RewardRuleResponseVo;
import com.powernode.rules.config.DroolsHelper;
import com.powernode.rules.service.RewardRuleService;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieSession;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class RewardRuleServiceImpl implements RewardRuleService {


    /**
     * 计算订单奖励
     */
    @Override
    public RewardRuleResponseVo calculateOrderRewardFee(RewardRuleRequestForm rewardRuleRequestForm) {
        //创建drools入参对象
        RewardRuleRequest rewardRuleRequest = new RewardRuleRequest();
        rewardRuleRequest.setOrderNum(rewardRuleRequestForm.getOrderNum());

        KieSession kieSession = DroolsHelper.loadForRule("rules/RewardRule.drl");

        //构建drools出参对象
        RewardRuleResponse rewardRuleResponse = new RewardRuleResponse();
        kieSession.setGlobal("rewardRuleResponse", rewardRuleResponse);

        //传入参数
        kieSession.insert(rewardRuleRequest);

        //触发规则
        kieSession.fireAllRules();
        //终止会话
        kieSession.dispose();

        RewardRuleResponseVo rewardRuleResponseVo = new RewardRuleResponseVo();
        rewardRuleResponseVo.setRewardRuleId(1L);
        rewardRuleResponseVo.setRewardAmount(rewardRuleResponse.getRewardAmount());//奖励金额

        return rewardRuleResponseVo;
    }
}
